package com.tengju.bff.infrastructure.export.convert;

import com.tengju.bff.interfaces.shared.ColumnVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.export.domain.record.Table;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: 李大仙
 * @date: 2021/8/23 4:49 下午
 */
public class TableVoConvert {

    public static Table convert(TableVO tableVO) {
        Table table = new Table();
        List<String> headList = new ArrayList<>();
        List<String> headPropList = new ArrayList<>();
        List<List<String>> dataList = new ArrayList<>();

        List<ColumnVO> columnVOList = tableVO.getHeader();
        for (ColumnVO columnVO : columnVOList) {
            headList.add(columnVO.getLabel());
            headPropList.add(columnVO.getProp());
        }
        table.setHeader(headList);

        List<Map<String, Object>> records = tableVO.getRecords();
        if (!CollectionUtils.isEmpty(records)){
            for (Map<String, Object> record : records) {
                List<String> row = new ArrayList<>();
                for (String prop : headPropList) {
                    row.add(String.valueOf(record.get(prop)));
                }
                dataList.add(row);
            }
        }
        table.setRecords(dataList);

        return table;
    }
}
